from typing import List

class Solution:
    def minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int:
        if n <= 1:
            return 0
        # parse edges:
        self.tree = {}
        for a, b in edges:
            if a in self.tree and b not in self.tree[a]:
                self.tree[a].append(b)
            else:
                self.tree[a] = [b]
            if b in self.tree and a not in self.tree[b]:
                self.tree[b].append(a)
            else:
                self.tree[b] = [a]
        res = sum([self._minTime(n, edges, hasApple, sub, 0) for sub in self.tree[0]])
        return res

    def _minTime(self, n: int, edges: List[List[int]], hasApple: List[bool], root, parent) -> int:
        res = 0
        subs = [n for n in self.tree[root] if n != parent]
        for sub in subs:
            res += self._minTime(n, edges, hasApple, sub, root)
        if hasApple[root] or res != 0:
            res += 2
        return res


if __name__ == '__main__':
    s = Solution()
    print(s.minTime(n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [False,False,True,False,True,True,False]))
